package cn.smile.mvc;

import cn.smile.interceptor.CustomInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * @必须打开MVC控制器 @EnableWebMvc
 * @Author: 毛苏宇
 * User: me
 * Date: 2021/4/22
 * Time: 13:07
 * Description:
 */
@Configuration
@EnableWebMvc
@ComponentScan("cn.smile.controller")
public class SpringMvcConfig{


    @Resource
    private CustomInterceptor customInterceptor;
    @Resource
    private CommonsMultipartResolver multipartResolver;

    /**
     * SpringMVC配置
     * @return
     */
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer=new WebMvcConfigurerAdapter() {
            /**
             * 配置视图解析器
             * @param registry
             */
            @Override
            public void configureViewResolvers(ViewResolverRegistry registry) {
                registry.jsp("/WEB-INF/views", ".jsp");
            }

            /**
             * 消息转换器，解决JSON中文乱码
             * @param converters
             */
            @Override
            public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                //字符编码转换
                StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
                stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
                //支持JSON返回,否则报错
                MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter=new MappingJackson2HttpMessageConverter();
                converters.add(stringHttpMessageConverter);
                converters.add(mappingJackson2HttpMessageConverter);
            }

            /**
             * 注册拦截器
             * @param registry
             */
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //registry.addInterceptor(customInterceptor).addPathPatterns("/**");
            }

            /**
             * 静态资源处理器
             * @param registry
             */
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/statics/**").addResourceLocations("/statics/");
            }

        };
        return webMvcConfigurer;
    }

}
